package com.jamlu.framework.ui.status;

import android.support.annotation.NonNull;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jamlu.framework.model.bean.BaseBean;
import com.jamlu.framework.model.bean.BaseListBean;
import com.jamlu.framework.presenter.IBaseRxPresenter;
import com.jamlu.framework.ui.view.IBaseView;

import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by DMing at 2018/3/2 0002
 */

public class BaseStatusView<T extends IBaseRxPresenter> implements IStatusView {

    private T presenter;
    private IBaseView iBaseView;

    public BaseStatusView(IBaseView iBaseView) {
        this.iBaseView = iBaseView;
    }

    public BaseStatusView setPresenter(T presenter){
        this.presenter = presenter;
        return this;
    }

    public T getPresenter() {
        return presenter;
    }

    @Override
    public IStatus injectStatusToast(@NonNull StatusRequest statusRequest){
       return StatusUtils.initStatusToast(iBaseView.getContext(),presenter.getDisposables(), statusRequest);
    }

    @Override
    public IStatus injectStatusDialog(@NonNull StatusRequest statusRequest){
        return StatusUtils.initStatusDialog(iBaseView, presenter.getDisposables(), true, statusRequest);
    }

    @Override
    public IStatus injectStatusDialog(boolean cancelable,@NonNull StatusRequest statusRequest){
        return StatusUtils.initStatusDialog(iBaseView, presenter.getDisposables(), cancelable, statusRequest);
    }

    @Override
    public IStatus injectStatusView(@NonNull ViewGroup viewGroup, @NonNull StatusRequest statusRequest){
        return StatusUtils.initStatusOfficialView(viewGroup,presenter.getDisposables(), statusRequest);
    }

    @Override
    public StatusSmartRvView injectStatusSmartRv(@NonNull ViewGroup viewGroup, @NonNull BaseQuickAdapter baseQuickAdapter, @NonNull StatusRequest statusRequest){
        return StatusUtils.initStatusSmartRvView(viewGroup, baseQuickAdapter, presenter.getDisposables(), statusRequest);
    }

    @Override
    public StatusSmartRvView hasDataSmartRvView(@NonNull ViewGroup viewGroup, @NonNull BaseQuickAdapter baseQuickAdapter, List list, StatusRequest statusRequest){
        return StatusUtils.hasDataSmartRvView(viewGroup, baseQuickAdapter,list, presenter.getDisposables(), statusRequest);
    }
    /////////////////////////////////////////////////////////////////////////////////////////////
    @Override
    public IStatus requestStatusDefine(@NonNull IStatus iStatus, @NonNull RequestObservable requestObservable){
        RequestUtils.request(requestObservable.getObservable())
                .iStatus(iStatus)
                .listener();
        return iStatus;
    }

    @Override
    public IStatus requestStatusToast(@NonNull final RequestObservable requestObservable){
        return injectStatusToast(new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listener();
            }
        });
    }

    @Override
    public IStatus requestStatusDialog(@NonNull final RequestObservable requestObservable){
        return injectStatusDialog(new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listener();
            }
        });
    }

    @Override
    public IStatus requestStatusDialog(boolean cancelable,@NonNull final RequestObservable requestObservable){
        return injectStatusDialog(cancelable,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listener();
            }
        });
    }

    @Override
    public IStatus requestStatusView(@NonNull ViewGroup viewGroup,@NonNull final RequestObservable requestObservable){
        return injectStatusView(viewGroup,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listener();
            }
        });
    }

    @Override
    public StatusSmartRvView requestStatusSmartRv(@NonNull ViewGroup viewGroup, @NonNull BaseQuickAdapter baseQuickAdapter, @NonNull final RequestObservable requestObservable){
        return injectStatusSmartRv(viewGroup,baseQuickAdapter,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listenerList();
            }
        });
    }

    @Override
    public StatusSmartRvView requestHasDataSmartRv(@NonNull ViewGroup viewGroup, @NonNull  BaseQuickAdapter baseQuickAdapter, @NonNull List list, @NonNull final RequestObservable requestObservable){
        return hasDataSmartRvView(viewGroup,baseQuickAdapter,list,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listenerList();
            }
        });
    }
    /////////////////////////////////////////////////////////////////////////////////////////////
    @Override
    public IStatus requestStatusDefine(@NonNull IStatus iStatus, @NonNull RequestObservable requestObservable, @NonNull  final Consumer<? extends BaseBean> consumer){
        RequestUtils.request(requestObservable.getObservable())
                .iStatus(iStatus)
                .listener(consumer);
        return iStatus;
    }

    @Override
    public IStatus requestStatusToast(@NonNull final RequestObservable requestObservable, @NonNull final Consumer<? extends BaseBean> consumer){
        return injectStatusToast(new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listener(consumer);
            }
        });
    }

    @Override
    public IStatus requestStatusDialog(@NonNull final RequestObservable requestObservable,@NonNull  final Consumer<? extends BaseBean> consumer){
        return injectStatusDialog(new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listener(consumer);
            }
        });
    }

    @Override
    public IStatus requestStatusDialog(boolean cancelable,@NonNull final RequestObservable requestObservable, @NonNull final Consumer<? extends BaseBean> consumer){
        return injectStatusDialog(cancelable,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listener(consumer);
            }
        });
    }

    @Override
    public IStatus requestStatusView(@NonNull ViewGroup viewGroup,@NonNull final RequestObservable requestObservable, @NonNull final Consumer<? extends BaseBean> consumer){
        return injectStatusView(viewGroup,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listener(consumer);
            }
        });
    }

    @Override
    public StatusSmartRvView requestStatusSmartRv(@NonNull ViewGroup viewGroup, @NonNull BaseQuickAdapter baseQuickAdapter, @NonNull final RequestObservable requestObservable, @NonNull final Consumer<? extends BaseListBean> consumer){
        return injectStatusSmartRv(viewGroup,baseQuickAdapter,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listenerList(consumer);
            }
        });
    }

    @Override
    public StatusSmartRvView requestHasDataSmartRv(@NonNull ViewGroup viewGroup, @NonNull  BaseQuickAdapter baseQuickAdapter, @NonNull List list, @NonNull final RequestObservable requestObservable, @NonNull  final Consumer<? extends BaseListBean> consumer){
        return hasDataSmartRvView(viewGroup,baseQuickAdapter,list,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listenerList(consumer);
            }
        });
    }

    /////////////////////////////////////////////////////////////////////////////////////////////
    @Override
    public IStatus requestStatusDefine(@NonNull IStatus iStatus, @NonNull RequestObservable requestObservable, @NonNull  Consumer<? extends BaseBean> consumer, @NonNull Consumer<Integer> failureStatus){
        RequestUtils.request(requestObservable.getObservable())
                .iStatus(iStatus)
                .listener(consumer,failureStatus);
        return iStatus;
    }

    @Override
    public IStatus requestStatusToast(@NonNull final RequestObservable requestObservable, @NonNull final Consumer<? extends BaseBean> consumer, @NonNull final Consumer<Integer> failureStatus){
        return injectStatusToast(new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listener(consumer,failureStatus);
            }
        });
    }

    @Override
    public IStatus requestStatusDialog(@NonNull final RequestObservable requestObservable, @NonNull final Consumer<? extends BaseBean> consumer, @NonNull  final Consumer<Integer> failureStatus){
        return injectStatusDialog(new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listener(consumer,failureStatus);
            }
        });
    }

    @Override
    public IStatus requestStatusDialog(boolean cancelable, @NonNull final RequestObservable requestObservable, @NonNull final Consumer<? extends BaseBean> consumer, @NonNull final Consumer<Integer> failureStatus){
        return injectStatusDialog(cancelable,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listener(consumer,failureStatus);
            }
        });
    }

    @Override
    public IStatus requestStatusView(@NonNull ViewGroup viewGroup , @NonNull final RequestObservable requestObservable, @NonNull final Consumer<? extends BaseBean> consumer, @NonNull final Consumer<Integer> failureStatus){
        return injectStatusView(viewGroup,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listener(consumer,failureStatus);
            }
        });
    }

    @Override
    public StatusSmartRvView requestStatusSmartRv(@NonNull ViewGroup viewGroup, @NonNull  BaseQuickAdapter baseQuickAdapter, @NonNull final RequestObservable requestObservable, @NonNull  final Consumer<? extends BaseListBean> consumer, @NonNull  final Consumer<Integer> failureStatus){
        return injectStatusSmartRv(viewGroup,baseQuickAdapter,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listenerList(consumer,failureStatus);
            }
        });
    }

    @Override
    public StatusSmartRvView requestHasDataSmartRv(@NonNull ViewGroup viewGroup, @NonNull  BaseQuickAdapter baseQuickAdapter, @NonNull List list, @NonNull final RequestObservable requestObservable, @NonNull  final Consumer<? extends BaseListBean> consumer, @NonNull  final Consumer<Integer> failureStatus){
        return hasDataSmartRvView(viewGroup,baseQuickAdapter,list,new StatusRequest() {
            @Override
            public Disposable run(@NonNull IStatus iStatus) {
                return RequestUtils.request(requestObservable.getObservable())
                        .iStatus(iStatus)
                        .listenerList(consumer,failureStatus);
            }
        });
    }

}
